package com.fenghuajueli.idiomppp.ui.fragment.home;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.ClassicStoryListAdapter;
import com.fenghuajueli.idiomppp.adapter.RecommendGridAdapter;
import com.fenghuajueli.idiomppp.base.JumpActivityUtils;
import com.fenghuajueli.idiomppp.config.BizCallBack;
import com.fenghuajueli.idiomppp.entity.IdiomStoryEntity;
import com.fenghuajueli.idiomppp.entity.VideoInfoEntity;
import com.fenghuajueli.idiomppp.ui.idiomcategory.IdiomCategoryActivity;
import com.fenghuajueli.idiomppp.ui.idiompin.IdiomSelectPassActivity;
import com.fenghuajueli.idiomppp.ui.idiomstory.IdiomStoryActivity;
import com.fenghuajueli.idiomppp.utils.RuntimeDataUtils;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.mvp.LifecycleBaseFragment;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import com.fenghuajueli.libbasecoreui.widget.NoScrollGridView;
import com.fenghuajueli.libbasecoreui.widget.NoScrollListView;


import java.util.List;


/**
 * 成语首页界面
 */
public class HomepageFragment extends LifecycleBaseFragment<HomepagePresenter> implements HomepageView {

    private RecommendGridAdapter recommendGridAdapter;

    NoScrollGridView recommendGridView;
    NoScrollListView classicStoryListView;
    TextView ivRecommend;
    TextView ivMoreRecommend;
    private ClassicStoryListAdapter classicStoryListAdapter;


    public static BizCallBack mCallback;

    @Override
    public HomepagePresenter initPresenter() {
        return new HomepagePresenter(getActivity());
    }


    @Override
    public void initView(Bundle savedInstanceState) {
        recommendGridView = (NoScrollGridView) findViewById(R.id.recommendGridView);
        classicStoryListView = (NoScrollListView) findViewById(R.id.classicStoryListView);
        ivRecommend = (TextView) findViewById(R.id.ivRecommend);
        ivMoreRecommend = (TextView) findViewById(R.id.ivMoreRecommend);

        presenter.loadData();
        presenter.initStoryData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_homepage;
    }


    public static HomepageFragment newInstance() {
        Bundle args = new Bundle();
        HomepageFragment fragment = new HomepageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void onClick(View view) {
        if (!QuickClickUtils.isFastClick(1000)) {
            return;
        }
        int id = view.getId();
        if (id == R.id.ivGoPass) {

            if(mCallback!=null){
                if(mCallback.onBiz(3)){
                    JumpActivityUtils.goNormalActivity(getActivity(), IdiomSelectPassActivity.class);
                }
            }else {
                JumpActivityUtils.goNormalActivity(getActivity(), IdiomSelectPassActivity.class);
            }

        } else if (id == R.id.ivLearnStory) {
                JumpActivityUtils.goNormalActivity(getActivity(), IdiomCategoryActivity.class, 1);
        } else if (id == R.id.ivMoreRecommend) {
                    JumpActivityUtils.goNormalActivity(getActivity(), IdiomCategoryActivity.class, 0);
        } else if (id == R.id.ivMoreStory) {
                    JumpActivityUtils.goNormalActivity(getActivity(), IdiomCategoryActivity.class, 1);
        }
    }


    @Override
    public void dataLoadSuccess(List<VideoInfoEntity> videoInfoEntities) {
        recommendGridAdapter = new RecommendGridAdapter(getActivity(), videoInfoEntities);
        recommendGridAdapter.setOnListClickListener(new OnListClickListener<VideoInfoEntity>() {
            @Override
            public void itemClick(int position, VideoInfoEntity data) {
                if(mCallback!=null){
                    if(mCallback.onBiz(position)){
                        //跳视频

                    }
                }else {
                    //跳视频

                }
            }
        });
        recommendGridView.setAdapter(recommendGridAdapter);
    }

    @Override
    public void storyLoadSuccess(List<IdiomStoryEntity> idiomStoryEntities) {
        classicStoryListAdapter = new ClassicStoryListAdapter(getActivity(), idiomStoryEntities);
        classicStoryListAdapter.setOnListClickListener((position, data) -> {
            RuntimeDataUtils.idiomStoryEntity = data;
            JumpActivityUtils.goNormalActivity(getContext(), IdiomStoryActivity.class);
        });
        classicStoryListView.setAdapter(classicStoryListAdapter);
    }

    @Override
    public void videoDataError() {
        ivRecommend.setVisibility(View.GONE);
        ivMoreRecommend.setVisibility(View.GONE);
        recommendGridView.setVisibility(View.GONE);
    }
}
